package activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.ilioili.proabslist.AbsLoadView;
import com.ilioili.proabslist.ListStateListener;
import com.ilioili.proabslist.ProAbsListView;
import com.laoxinwen.app.R;
import com.umeng.analytics.MobclickAgent;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Random;

import activity.discuss.DiscussActivity;
import activity.main.MainActivity;
import bean.News;
import bean.TabTopicItem;
import bean.Topic;
import bean.TopicArticleItem;
import bean.TopicNewsItem;
import bean.wraper.DefaultStringWrapper;
import bean.wraper.TopicWrapper;
import cn.evun.proabslistview.FreshView;
import cn.evun.proabslistview.LoadView;
import common.Constants;
import common.DateUtil;
import common.FastAdapter;
import common.HttpCallBack;
import common.MyApp;
import common.Requester;
import common.ServerConfig;
import common.UserConfig;
import common.UserGuideUtil;
import share.LoginHelper;
import share.LoginType;
import share.ShareActivity;
import share.ShareDialog;
import util.ToastUtil;
import util.bitmap.FileUtil;
import util.bitmap.Img;

@SuppressLint("SimpleDateFormat")
public class TopicActivity extends ShareActivity implements View.OnClickListener, ListStateListener, LoginHelper.LoginResultListener {
    public static final String ACTION_WITH_DATA = "with_data";
    public static final String ACTION_WITH_ID = "with+id";
    public static final String INTENT_KEY_NEWS_GROUP_OBJ = "NewsGroupObj";
    public static final String INTENT_KEY_NEWS_GROUP_ID = "NewsGroupId";
    public static final String INTENT_KEY_SHOW_ARTICLE = "showArticle";

    private static final int TAB_NEWS = 0;
    private static final int TAB_ARTICLE = 1;

    private long createTime;
    private Handler handler = new Handler();

    public static Intent getIntent(Context context, String newsGroupId) {
        return getIntent(context, newsGroupId, false);
    }

    public static Intent getIntent(Context context, String newsGroupId, boolean showArticle) {
        Intent it = new Intent(context, TopicActivity.class);
        it.putExtra(INTENT_KEY_NEWS_GROUP_ID, newsGroupId);
        it.putExtra(INTENT_KEY_SHOW_ARTICLE, showArticle);
        it.setAction(ACTION_WITH_DATA);
        return it;
    }
    
    private View headView;
    private ListView listView;
    private LoginHelper loginHelper;

    private boolean isDataLoaded;
    //当前浏览专题id
    private String topicId;
    private Topic topic;
    private ArrayList<TopicNewsItem> filteredNewsList;
    private boolean isInteractRTopicList;
    private boolean modeKeyNode = true;
    private boolean modeTimeDesc = false;

    @id(R.id.proAbsListView)
    private ProAbsListView proAbsListView;
    private Dialog shareDialog;
    @id(R.id.tv_num)
    private TextView tvNum;
    private TextView tvSummary;
    @id(R.id.tv_topic_title)
    private TextView tvTitle;
    private ImageView vBanner;
    private ViewGroup vBannerContainerInHeadView;
    @id(R.id.out_banner_container)
    private ViewGroup vBannerContainerInOutView;
    @id(R.id.bottom_bar)
    private View vBottomBar;
    @id(R.id.iv_loading)
    private ProgressBar ivloading;
    private View rtopicCtn;

    @id(R.id.bt_care)
    private View vCollection;
    private View vTab;
    private View vTabArticle;
    private ViewGroup vTabContainerInHeadView;
    @id(R.id.out_tab_container)
    private ViewGroup vTabContainerInOutView;
    private View vTabNews;
    @id(R.id.bt_keynode)
    private View btnKeyNode;
    @id(R.id.bt_sort)
    private View btnSort;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_topic);
        loadView();
        listView = (ListView) proAbsListView.getAbsListView();
        FreshView freshView = new FreshView(this) {
            @Override
            public View getFreshView() {
                View v = super.getFreshView();
                v.setVisibility(View.GONE);
                return v;
            }
        };
        proAbsListView.setFreshView(freshView);
        proAbsListView.setFreshable(false);

        AbsLoadView loadView = new LoadView(this) {
            @Override
            public View getLoadView() {
                View v = super.getLoadView();
                v.setVisibility(View.GONE);
                return v;
            }

        };
        proAbsListView.setLoadView(loadView);
        proAbsListView.setLoadable(false);
        proAbsListView.setListStateListener(this);
        topicId = getIntent().getStringExtra(INTENT_KEY_NEWS_GROUP_ID);
        //如果已经读过该专题了，自动倒序并显示所有节点
        if(UserConfig.isReadedTopic(topicId)) {
            modeKeyNode = false;
            modeTimeDesc = true;
        }
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                proAbsListView.startFresh();
            }
        });
        createTime = System.currentTimeMillis();
        loginHelper = new LoginHelper(this);
        loginHelper.setLoginResultListener(this);
        //        new Handler().postDelayed(hideBottomBar, 2000);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("专题详情页");
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("专题详情页");
    }

    @Override
    protected void checkSlideGesture(MotionEvent ev) {
        if (!isInteractRTopicList) {
            super.checkSlideGesture(ev);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (null != topic) {
            HashMap<String, String> map = new HashMap<>();
            map.put("TopicId", topicId);
            map.put("Title", topic.getTitle());
            MobclickAgent.onEventValue(this, Constants.UMENG_EVENT_TOPIC, map, (int) (System.currentTimeMillis() - createTime));
        }
    }

    @Override
    protected void onRightSlide() {
        finish();
    }

    @Override
    protected void onLeftSlide() {
        startActivity(CommentActivity.getIntent(this, topicId, null, null, null));
    }

    @Override
    protected void onDownSlide() {
        if (vBottomBar.getVisibility() == View.GONE) {
            handler.post(showBottomBar);
        }
    }

    @Override
    protected void onUpSlide() {
        if (vBottomBar.getVisibility() == View.VISIBLE) {
            handler.post(hideBottomBar);
        }
    }

    @Override
    public void finish() {
        if (this.isTaskRoot()) {
            Intent it = new Intent(this, MainActivity.class);
            startActivity(it);
        }
        super.finish();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_share:
                shareTopic();
                break;
            case R.id.bt_back:
                finish();
                break;
            case R.id.bt_comment:
                startActivity(CommentActivity.getIntent(this, topic.getId(), null, null, null));
                break;
            case R.id.bt_h5:
                startActivity(H5TopicActivity.getIntent(this, topic));
                break;
            case R.id.bt_keynode:
                modeKeyNode = !modeKeyNode;
                if (isDataLoaded) {
                    filterNewsList();
                    newsAdapter.notifyDataSetChanged();
//                    listView.setAdapter(newsAdapter);
                }
                if (modeKeyNode) {
                    showToast("已切换至只显示关键新闻");
                } else {
                    showToast("已切换至显示所有新闻");
                }
                break;
            case R.id.bt_sort:
                modeTimeDesc = !modeTimeDesc;
                if (isDataLoaded) {
                    filterNewsList();
                    newsAdapter.notifyDataSetChanged();
//                    listView.setAdapter(newsAdapter);
                }
                if (modeTimeDesc) {
                    showToast("已切换至倒序显示");
                } else {
                    showToast("已切换至顺序显示");
                }
                break;
            case R.id.bt_care:
                if (!UserConfig.isLogined()) {
                    showLoginDialog();
                } else {
                    Requester.markCareTopic(topic.getId(), new HttpCallBack<DefaultStringWrapper>() {
                        @Override
                        public void onSucceed(DefaultStringWrapper data) {
                            //                        topic.setAttention(topic.getAttention() == 0 ? 1 : 0);
                            if (vCollection.isSelected()) {
                                ToastUtil.showShortToast("已取消关注");
                            } else {
                                ToastUtil.showShortToast("已关注");
                            }

                            vCollection.setSelected(!vCollection.isSelected());
                        }

                        @Override
                        protected void onServerError(DefaultStringWrapper data) {
                            super.onServerError(data);
                            MainActivity.jumpHereForLogin(TopicActivity.this);
                        }

                        @Override
                        protected void onNetError() {
                            super.onNetError();
                        }
                    });
                }

                break;
            case R.id.tab_discuss:
                Intent intent = DiscussActivity.getIntent(this, topic);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onScrollWhileFreshViewVisible(int percentage) {

    }

    @Override
    public void onScrollWhileLoadViewVisible(int percentage) {

    }

    @Override
    public void onStartFreshing() {
        if (!isDataLoaded) {
            requestData();
        } else {
            proAbsListView.cancelFreshing();
        }
    }

    @Override
    public void onStartLoading() {
        proAbsListView.cancelLoading();
    }

    private void requestData() {
        isDataLoaded = false;
        ivloading.setVisibility(View.VISIBLE);
        Requester.getNewsGroup(topicId, new HttpCallBack<TopicWrapper>() {
            @Override
            public void onSucceed(TopicWrapper t) {
                topic = t.getData();
                proAbsListView.cancelFreshing();
                proAbsListView.cancelLoading();
                isDataLoaded = true;
                filterNewsList();
                setData();
//                handler.post(showBottomBar);
                ivloading.setVisibility(View.GONE);
                //记录readed
                UserConfig.setReadedTopic(topicId);
            }

            @Override
            protected void onNetError() {
                super.onNetError();
                isDataLoaded = false;
                proAbsListView.cancelFreshing();
                proAbsListView.cancelLoading();
                ivloading.setVisibility(View.GONE);
            }

            @Override
            protected void onServerError(TopicWrapper data) {
                super.onServerError(data);
                isDataLoaded = false;
                proAbsListView.cancelFreshing();
                proAbsListView.cancelLoading();
                ivloading.setVisibility(View.GONE);
            }

        });

    }

    private void setData() {
        headView = getLayoutInflater().inflate(R.layout.topic_header, listView, false);
        tvSummary = f(R.id.tv_topic_summary, headView);
        //tvTitle = f(R.id.tv_topic_title, headView);
        addSpaceFootView();
        listView.addHeaderView(headView);
        //        tvComment.setText(topic.getCommentsNum());
        tvTitle.setText(topic.getTitle());
        tvSummary.setText(topic.getSummary());
        vTabArticle = f(R.id.tab_article, headView);
        vTabNews = f(R.id.tab_news, headView);
        vTab = f(R.id.newsgroup_tab, headView);
        vTabContainerInHeadView = f(R.id.in_tab_container, headView);
        vBannerContainerInHeadView = f(R.id.in_banner_container, headView);
//        vTabNews.setSelected(true);
        vCollection.setSelected("1".equals(topic.getAttention()));
        if ("0".equals(topic.getComments_num())) {
            tvNum.setVisibility(View.GONE);
        } else {
            tvNum.setVisibility(View.VISIBLE);
            if (topic.getComments_num().length() > 2) {
                tvNum.setText("99+");
            } else {
                tvNum.setText(topic.getComments_num());
            }
        }

        vTabNews.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(final View v) {
                switchTab(TAB_NEWS);
            }
        });
        vTabArticle.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(final View v) {
                switchTab(TAB_ARTICLE);
            }
        });

        vBanner = f(R.id.banner_view, headView);
        if (null == topic.getBanner()) {
            vBanner.setVisibility(View.GONE);
        } else {
            vBanner.setVisibility(View.VISIBLE);
            imgPool.bind(getImg(topic.getBanner()), vBanner);
        }
        tvSummary.setText("            " + topic.getSummary());

//        listView.setAdapter(newsAdapter);
        if( getIntent().getBooleanExtra(INTENT_KEY_SHOW_ARTICLE, false) ) {
            switchTab(TAB_ARTICLE);
        }
        else {
            switchTab(TAB_NEWS);
        }
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (headView.getTop() + vTabContainerInHeadView.getTop() < vBannerContainerInOutView.getMeasuredHeight()) {
                    if (vTabContainerInHeadView.getChildCount() != 0) {
                        vTabContainerInHeadView.removeView(vTab);
                        vTabContainerInOutView.addView(vTab);
                    }
                } else {
                    if (vTabContainerInHeadView.getChildCount() == 0) {
                        vTabContainerInOutView.removeView(vTab);
                        vTabContainerInHeadView.addView(vTab);
                    }
                }
            }
        });
        //使用引导
        ViewGroup rootView = (ViewGroup) f(R.id.topic_root);
        boolean guideTest = UserGuideUtil.checkGuide(UserGuideUtil.GUIDE_TOPIC_DISCUSS, rootView) || UserGuideUtil.checkGuide(UserGuideUtil.GUIDE_TOPIC_H5, rootView) || UserGuideUtil.checkGuide(UserGuideUtil.GUIDE_TOPIC_KEYNODE, rootView) || UserGuideUtil.checkGuide(UserGuideUtil.GUIDE_TOPIC_SORT, rootView) || UserGuideUtil.checkGuide(UserGuideUtil.GUIDE_TOPIC_FAVOR, rootView);
    }

    private void switchTab(int tab) {
        if(tab == TAB_NEWS) {
            if(!vTabNews.isSelected()) {
                vTabArticle.setSelected(false);
                vTabNews.setSelected(true);
                listView.setAdapter(newsAdapter);
                btnKeyNode.setVisibility(View.VISIBLE);
                btnSort.setVisibility(View.VISIBLE);
            }
        }
        else {
            if(!vTabArticle.isSelected()) {
                vTabArticle.setSelected(true);
                vTabNews.setSelected(false);
                listView.setAdapter(articleAdapter);
                btnKeyNode.setVisibility(View.GONE);
                btnSort.setVisibility(View.GONE);
            }
        }
    }

    private void addSpaceFootView() {
        View footView = new View(this);
        footView.setLayoutParams(new AbsListView.LayoutParams(0, (int) (30 * getResources().getDisplayMetrics().density)));
        listView.addFooterView(footView);
    }

    private void filterNewsList() {
        //根据mode过滤列表
        filteredNewsList = new ArrayList<>();
        for (TopicNewsItem news : topic.getNews()) {
            if (modeKeyNode) {
                if ("1".equals(news.getKeynode())) {
                    if (modeTimeDesc) {
                        filteredNewsList.add(news);
                    } else {
                        filteredNewsList.add(0, news);
                    }
                }
            } else {
                if (modeTimeDesc) {
                    filteredNewsList.add(news);
                } else {
                    filteredNewsList.add(0, news);
                }
            }
        }
    }

    private FastAdapter newsAdapter = new FastAdapter() {
        @Override
        public int getCount() {
            if (modeKeyNode) {
                int count = 0;
                for (TopicNewsItem news : topic.getNews()) {
                    if ("1".equals(news.getKeynode())) {
                        count++;
                    }
                }
                return count + (topic.getRtopics().isEmpty() ? 0 : 1);
            } else {
                return topic.getNews().size() + (topic.getRtopics().isEmpty() ? 0 : 1);
            }
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (position == filteredNewsList.size()) {
                return initRTopicView(parent);
            }
            TopicNewsItem newsItem = filteredNewsList.get(position);

            Holder holder = null;
            if (null == convertView) {
                convertView = LayoutInflater.from(TopicActivity.this).inflate(R.layout.topic_news_item, parent, false);
                convertView.setClickable(true);
                convertView.setOnClickListener(listenerForNews);
                holder = new Holder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            holder.setData(newsItem, position);
            return convertView;
        }

        @Override
        public int getItemViewType(int position) {
            if (position == filteredNewsList.size()) {
                return 1;
            }
            return 0;
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }
    };
    private OnClickListener listenerForNews = new OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView tv = f(R.id.tv_news_title, v);
            tv.setTextColor( MyApp.getMyTheme().getThemeColor(R.attr.font_minor_color) );

            TopicNewsItem newsItem = (TopicNewsItem) v.getTag(2 << 24);
            News news = new News();
            news.setId(newsItem.getId());
            news.setTid(topic.getId());
            news.setTitle(newsItem.getTitle());
            news.setSummary(topic.getTitle());
            news.setImg(newsItem.getImg());
            Intent it = NewsActivity.getIntent(TopicActivity.this, news, topic);
            startActivity(it);
        }
    };
    private OnClickListener listenerForArticle = new OnClickListener() {
        @Override
        public void onClick(View v) {
            TextView tv = f(R.id.tv_news_title, v);
            tv.setTextColor( MyApp.getMyTheme().getThemeColor(R.attr.font_minor_color) );

            TopicArticleItem articleItem = (TopicArticleItem) v.getTag(2 << 24);
            News article = new News();
            article.setId(articleItem.getId());
            article.setTid(topic.getId());
            article.setTitle(articleItem.getTitle());
            article.setSummary(topic.getSummary());
            article.setImg(articleItem.getImg());
            Intent it = ArticleActivity.getIntent(TopicActivity.this, article, topic);
            startActivity(it);
        }
    };
    private OnClickListener listenerForRTopic = new OnClickListener() {
        @Override
        public void onClick(View v) {
            String topicId = (String) v.getTag();
            Intent it = TopicActivity.getIntent(TopicActivity.this, topicId);
            it.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
            startActivity(it);
        }
    };
    private FastAdapter articleAdapter = new FastAdapter() {
        @Override
        public int getCount() {
            return topic.getArticles().size() + (topic.getRtopics().isEmpty() ? 0 : 1);
        }

        @Override
        public View getView(int position, View v, ViewGroup parent) {
            if (position == topic.getArticles().size()) {
                return initRTopicView(parent);
            }

            TopicArticleItem article = topic.getArticles().get(position);
            if (null == v) {
                v = LayoutInflater.from(TopicActivity.this).inflate(R.layout.topic_article_item, parent, false);
                v.setOnClickListener(listenerForArticle);
                v.setClickable(true);
            }
            v.setTag(2 << 24, article);
            TextView tv = f(R.id.tv_news_title, v);
            tv.setText(article.getTitle());
            TextView tvMedia = f(R.id.tv_news_src, v);
            tvMedia.setText(article.getMedia() + "  " + DateUtil.autoFormat(article.getTime()));
            ImageView iv = f(R.id.iv_news_pic, v);
            imgPool.bind(getImg(article.getImg()), iv);
            //标题颜色
            tv.setTextColor(UserConfig.isReadedArticle(article.getId()) ? MyApp.getMyTheme().getThemeColor(R.attr.font_minor_color) : MyApp.getMyTheme().getThemeColor(R.attr.font_main_color));
            return v;
        }

        @Override
        public int getItemViewType(int position) {
            if (position == topic.getArticles().size()) {
                return 1;
            }
            return 0;
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }
    };

    /**
     * 初始化相关专题view
     *
     * @param parent
     * @return
     */
    private View initRTopicView(ViewGroup parent) {
        if (rtopicCtn == null) {
            rtopicCtn = LayoutInflater.from(this).inflate(R.layout.related_topiclist, parent, false);
            ViewGroup topicList = (ViewGroup) rtopicCtn.findViewById(R.id.rtopic_list);
            //加入相关专题卡片
            for (TabTopicItem rtopic : topic.getRtopics()) {
                View card = LayoutInflater.from(this).inflate(R.layout.related_topicitem, topicList, false);
                card.setTag(rtopic.getId());
                TextView tv = f(R.id.rtopic_title, card);
                tv.setText(rtopic.getTitle());
                ImageView iv = f(R.id.rtopic_img, card);
                imgPool.bind(getImg(rtopic.getImg()), iv);

                card.setClickable(true);
                card.setOnClickListener(listenerForRTopic);
                topicList.addView(card);
            }
            //横线滚动与activity冲突
            ((ViewGroup) topicList.getParent()).setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    isInteractRTopicList = (event.getAction() != MotionEvent.ACTION_UP);
                    return false;
                }
            });
        }
        return rtopicCtn;
    }

    private Runnable showBottomBar = new Runnable() {
        @Override
        public void run() {
            AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
            alphaAnimation.setDuration(250);
            alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    vBottomBar.setVisibility(View.VISIBLE);
                }

                @Override
                public void onAnimationEnd(Animation animation) {

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            vBottomBar.startAnimation(alphaAnimation);
        }
    };

    private Runnable hideBottomBar = new Runnable() {
        @Override
        public void run() {
            AlphaAnimation alphaAnimation = new AlphaAnimation(vBottomBar.getAlpha(), 0);
            alphaAnimation.setDuration(250);
            alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                    vBottomBar.setVisibility(View.GONE);
                }

                @Override
                public void onAnimationEnd(Animation animation) {

                }

                @Override
                public void onAnimationRepeat(Animation animation) {

                }
            });
            vBottomBar.startAnimation(alphaAnimation);
        }
    };

    private void shareTopic() {
        String url = ServerConfig.SERVER_HOST + "/topic/" + topic.getId();
        String userId = UserConfig.isLogined() ? UserConfig.getUserInfo().getId() : "0";
        Random r = new Random(System.currentTimeMillis());
        int random = r.nextInt(Integer.MAX_VALUE) + 1000;
        url += "?stamp=" + userId + "_" + System.currentTimeMillis() + random;
        final String srcUrl = url;
        shareDialog = ShareDialog.getShareDialog(this, new OnClickListener() {
            @Override
            public void onClick(View v) {
                shareDialog.dismiss();
                switch (v.getId()) {
                    case R.id.share_qq:
                        shareToQq(topic.getTitle(), topic.getSummary(), ServerConfig.PIC_HOST + topic.getImg(), srcUrl);
                        break;
                    case R.id.share_qq_zone:
                        shareToQqZone(topic.getTitle(), topic.getSummary(), ServerConfig.PIC_HOST + topic.getImg(), srcUrl);
                        break;
                    case R.id.share_weibo:
                        shareToWeibo(topic.getTitle(), topic.getSummary(), ServerConfig.PIC_HOST + topic.getImg(), srcUrl);
                        break;
                    case R.id.share_weixin:
                        shareToWeixin(topic.getTitle(), topic.getSummary(), ServerConfig.PIC_HOST + topic.getImg(), srcUrl);
                        break;
                    case R.id.share_weixin_friends:
                        shareToWeixinFriendCircle(topic.getTitle(), topic.getSummary(), ServerConfig.PIC_HOST + topic.getImg(), srcUrl);
                        break;
                    case R.id.share_url:
                        shareCopyUrl(srcUrl);
                        break;
                }
                Requester.shareLog("topic", topic.getId(), new HttpCallBack<DefaultStringWrapper>() {
                    @Override
                    public void onSucceed(DefaultStringWrapper data) {

                    }
                });
            }
        });
        shareDialog.show();
    }

    private void showLoginDialog() {
        loginHelper.showDialog();
    }

    @Override
    public void onLoginFail() {
        loginHelper.dismissDialog();
    }

    @Override
    public void onLoginSucceed() {
        loginHelper.dismissDialog();
    }

    @Override
    protected void onQqInfoObtained() {
        loginHelper.login(LoginType.QQ);
    }

    @Override
    protected void onWeiboInfoObtained() {
        loginHelper.login(LoginType.SINA);
    }

    @Override
    protected void onWechatInfoObtain() {
        loginHelper.login(LoginType.WECHAT);
    }

    private class Holder {
        View vRoot;
        TextView tvDate;
        TextView tvTitle;
        TextView tvYear;
        ImageView ivPic;
        View vLineDown;
        View vYearDividerBlock;
        Calendar date = Calendar.getInstance();

        Holder(View v) {
            vRoot = v;
            tvDate = f(R.id.tv_date, v);
            tvTitle = f(R.id.tv_news_title, v);
            tvYear = f(R.id.tv_year, v);
            ivPic = f(R.id.iv_news_pic, v);
            vLineDown = f(R.id.line_down, v);
            vYearDividerBlock = f(R.id.year_divider_block, v);
        }

        void setData(TopicNewsItem news, int position) {
            vRoot.setTag(2 << 24, news);
            date.setTimeInMillis(news.getTime());
            int year = date.get(Calendar.YEAR);
            int month = date.get(Calendar.MONTH) + 1;
            int day = date.get(Calendar.DAY_OF_MONTH);

            tvTitle.setText(news.getTitle());
            tvDate.setText(String.format("%02d", month) + '/' + String.format("%02d", day));
            if ("1".equals(news.getKeynode())) {
                tvDate.setBackgroundColor(getResources().getColor(R.color.tl_datebg_keynode));
            } else {
                tvDate.setBackgroundColor(getResources().getColor(R.color.tl_datebg));
            }
            tvTitle.getPaint().setFakeBoldText(false);
            tvTitle.setSelected(false);

            if ("".equals(news.getImg())) {
                ivPic.setVisibility(View.GONE);
            } else {
                ivPic.setVisibility(View.VISIBLE);
                imgPool.bind(getImg(news.getImg()), ivPic);
            }

            if (position == 0) {
                vYearDividerBlock.setVisibility(View.VISIBLE);
                tvYear.setText("" + year);
            } else {
                TopicNewsItem last = filteredNewsList.get(position - 1);
                date.setTimeInMillis(last.getTime());
                if (date.get(Calendar.YEAR) != year) {
                    vYearDividerBlock.setVisibility(View.VISIBLE);
                    tvYear.setText("" + year);
                } else {
                    vYearDividerBlock.setVisibility(View.GONE);
                }
            }
            //标题颜色
            tvTitle.setTextColor(UserConfig.isReadedNews(news.getId()) ? MyApp.getMyTheme().getThemeColor(R.attr.font_minor_color) : MyApp.getMyTheme().getThemeColor(R.attr.font_main_color) );
        }


    }
}
